package com.example.allinone.homework08.fragment1;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.allinone.R;
import com.example.allinone.homework08.User;

public class Fragment1 extends Fragment implements View.OnClickListener{

    private Button imp ;
    private Button sel ;
    private TextView username ;
    private TextView age ;
    private TextView result ;
    private TextView low ;
    private TextView high ;

    public Fragment1(){
        super(R.layout.fragment1);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        imp = (Button) getActivity().findViewById(R.id.imp);
        sel = (Button) getActivity().findViewById(R.id.sel);
        username = (TextView) getActivity().findViewById(R.id.username) ;
        age = (TextView) getActivity().findViewById(R.id.age) ;
        result = (TextView) getActivity().findViewById(R.id.result) ;
        low = (TextView)getActivity().findViewById(R.id.low) ;
        high = (TextView)getActivity().findViewById(R.id.high) ;
        imp.setOnClickListener(this);
        sel.setOnClickListener(this);
    }

    private User getUser(){
        String use = username.getText().toString() ;
        try{
            int ag = Integer.parseInt(age.getText().toString()) ;
            return new User(use,ag);
        }catch (NumberFormatException e){
            return null ;
        }
    }

    private String getSelection(){
        Integer hi = "".equals(high.getText().toString()) ? 100 : Integer.parseInt(high.getText().toString());
        Integer lo = "".equals(low.getText().toString()) ? 0 : Integer.parseInt(low.getText().toString());
        String selection = "age between "+lo+" and "+hi ;
        System.out.println(selection);
        return selection ;
    }

    void insertUser(User user){
        DbHelper dbHelper = new DbHelper(getContext()) ;
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues( );
        values.put("username" , user.getUsername());
        values.put("age" , user.getAge());
        long res = db.insert("user",null,values) ;
        String msg = res==-1 ? "加入错误" : "加入成功" ;
        Toast toast = Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT) ;
        toast.show();
    }

    void selectUser(){
        DbHelper dbHelper = new DbHelper((getContext()));
        SQLiteDatabase db = dbHelper.getReadableDatabase() ;
        String selection = getSelection() ;;
        Cursor cursor = db.query("user",null,selection,null,null,null,null);
        StringBuilder stringBuilder = new StringBuilder();
        int counter = 0 ;
        while (cursor.moveToNext()){
            counter++ ;
            long id = cursor.getLong(cursor.getColumnIndex("id")) ;
            String username = cursor.getString(cursor.getColumnIndex("username")) ;
            int age = cursor.getInt(cursor.getColumnIndex("age")) ;
            stringBuilder.append("id :").append(id).append("  username:").append(username).append("  age:").append(age).append("\n") ;
        }
        Toast.makeText(getContext(),"一共有"+counter+"条记录：\n"+stringBuilder.toString(),Toast.LENGTH_SHORT) ;
        result.setText("一共有"+counter+"条记录：\n"+stringBuilder.toString());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imp:
                User user = getUser() ;
                if(null!=user){
                    insertUser(user);
                }else {
                    Toast.makeText(getContext(),"用户输入错误",Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.sel:
                selectUser();
                break ;
        }
    }
}
